// pages/open/open.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    question: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    var self = this
    wx.request({
      url: app.globalData.baseUrl + 'users/query',
      data: {
        id: options.id,
        session: wx.getStorageSync("session")
      },
      method: "POST",
      success: function (res) {
        if (res.data.code === '000000' && res.data.body.length > 0) {
          self.setData({
            id: res.data.body[0].id,
            question: res.data.body[0].question
          })
        }
      }
    })
  },
  bindKeyInput: function (e) {
    this.setData({
      answer: e.detail.value
    })
  },
  check: function (options) {
    var self = this;
    wx.request({
      url: app.globalData.baseUrl + 'users/open',
      data: {
        session: wx.getStorageSync("session"),
        id: self.data.id,
        answer: self.data.answer
      },
      method: 'POST',
      success: function (res) {
        if (res.data.code === '000000') {
          wx.redirectTo({
            url: '../detail/detail?id=' + self.data.id
          })
        } else if (res.data.code === '200000') {
          console.log(res.data.msg);
          wx.showToast({
            title: '答案错误',
            icon: 'success',
            duration: 2000
          })
        } else {
          console.log(res.data.msg);
          wx.showToast({
            title: '邮件错误',
            icon: 'success',
            duration: 1000,
            success: function() {
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 1000)
            }
          })
          
        }
      }
    })
  }
})